<?php
/*
 * @开发工具: JetBrains PhpStorm.
 * @文件名：User.class.php
 * @类功能: 用户控制器
 * @开发者: 陈旭林
 * @开发时间： 2017-09-01
 * @版本：version 1.0
 */

namespace app\api\controller;

use app\admin\widget\Name;
use app\common\cache\car\ICarBrandCache;
use app\common\cache\car\ICarConfigCache;
use app\common\cache\car\ICarModelCache;
use app\common\controller\ApiBaseController;
use app\common\enum\AssetPayType;
use app\common\enum\CarStatus;
use app\common\enum\HolderType;
use app\common\enum\HtmlEnumValue;
use app\common\enum\IsCar;
use app\common\enum\IsDelete;
use app\common\enum\IsHistory;
use app\common\enum\IsMortgage;
use app\common\ext\IDb;
use app\common\ext\User;

class UserCar extends ApiBaseController{

    /**
     * @功能：车辆品牌
     * @开发者：cxl
     */
    public function car_brand(){
        //初期化数据
        $car_brand_array = array();

        //取得车辆品牌列表
        $car_brand_list = ICarBrandCache::getList();
        if(!empty($car_brand_list) && is_array($car_brand_list)){
            //整理数据
            foreach($car_brand_list as $item){
                //取得首字符
                if(empty(trim($item['car_brand_name_py']))){
                    $car_brand_name_py = "Other";
                }else{
                    $car_brand_name_py = strtoupper(substr($item['car_brand_name_py'],0,1));
                }

                //设置首字符
                $item['car_brand_name_py'] = $car_brand_name_py;

                //设置数据
                $car_brand_array[$car_brand_name_py][] = $item;
            }
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("car_brand_list",$car_brand_array);
        self::send();
    }

    /**
     * @功能：车辆型号
     * @开发者：cxl
     */
    public function car_model(){
        //接收参数
        $car_brand_id  = self::get_data('car_brand_id',self::NOT_EMPTY,"车辆品牌编码不能为空！");

        //取得车辆品牌列表
        $car_model_list = ICarModelCache::getList($car_brand_id);
        if(empty($car_model_list)){
            $car_model_list = array();
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("car_model_list",$car_model_list);
        self::send();
    }

    /**
     * @功能：车辆配置
     * @开发者：cxl
     */
    public function car_config(){
        //接收参数
        $car_brand_id  = self::get_data('car_brand_id');
        $car_model_id  = self::get_data('car_model_id',self::NOT_EMPTY,"车辆型号编码不能为空！");

        //取得车辆品牌列表
        $car_config_list = ICarConfigCache::getList($car_brand_id,$car_model_id);
        if(empty($car_config_list)){
            $car_config_list = array();
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("car_config_list",$car_config_list);
        self::send();
    }

    /**
     * @功能：个人车辆列表
     * @开发者：cxl
     */
    public function car_list(){
        //接收参数
        $user_id = self::$uid;

        //取得车辆列表
        $user_car_where['is_delete'] = IsDelete::No;
        $user_car_where['is_history'] = IsHistory::not;
        $user_car_where['user_id'] = $user_id;
        $user_car_list = IDb::getInstance("asset_car")->setDbWhere($user_car_where)->setDbOrder("user_car_id desc")->sel();

        //判断车辆是否存在
        if(!empty($user_car_list) && is_array($user_car_list) && count($user_car_list) > 0) {
            //整理数据
            foreach($user_car_list as &$data){
                $data['car_brand_name'] = Name::car_brand($data['car_brand_id']);
                $data['car_model_name'] = Name::car_model($data['car_model_id']);
                $data['car_config_name'] = Name::car_config($data['car_config_id']);

                if($data['holder_type'] != HolderType::Other){
                    if(isset(HtmlEnumValue::$enum_value['holder_type'][$data['holder_type']])){
                        $data['holder_name'] = HtmlEnumValue::$enum_value['holder_type'][$data['holder_type']];
                    }
                }
            }
        }else{
            $user_car_list = array();
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("user_car_list",$user_car_list);
        self::send();
    }

    /**
     * @功能：添加个人车辆
     * @开发者：cxl
     */
    public function car_add(){
        //接收参数
        $user_id = self::$uid;
        $holder_type  = self::get_data('holder_type',self::INT,'请选择车辆所有人');
        $holder_name  = self::get_data('holder_name');
        $car_brand_id  = self::get_data('car_brand_id',self::INT,'请选择车辆品牌');
        $car_model_id  = self::get_data('car_model_id',self::INT,'请选择车辆型号');
        $car_config_id  = self::get_data('car_config_id',self::INT,'请选择车辆配置');
        $run_mileage  = self::get_param('run_mileage',false,self::DOUBLE,'车辆行驶公里数');
        $car_money  = self::get_param('car_money',true,self::DOUBLE,'裸车价格');
        $buy_time  = self::get_data('buy_time',self::NOT_EMPTY,'请选择购车时间');
        $pay_type  = self::get_data('pay_type',self::INT,'请选择是否有按揭');
        $is_mortgage  = self::get_data('is_mortgage',self::INT,'请选择是否有抵押');
        $licence_url  = self::get_data('licence_url');

        //长度限制
        $car_money = self::data_filter($car_money,self::INT,'裸车价格');
        $run_mileage = self::data_filter($run_mileage,self::INT,'车辆行驶公里数');

        //判断是否设置空值
        if(trim($buy_time) == "") $buy_time = null;
        if(trim($licence_url) == "") $licence_url = null;

        if($holder_type == HolderType::Other){
            //所有人为其它，则取填写的值
            $user_car_data['holder_name'] = $holder_name;
        }

        if($pay_type == AssetPayType::installment){
            //按揭购买

            //数据接收
            $installment_loan_money  = self::get_param('installment_loan_money',true,self::NOT_EMPTY,'贷款金额');
            $installment_loan_cycle  = self::get_param('installment_loan_cycle',true,self::INT,'贷款周期');
            $installment_month_repay  = self::get_param('installment_month_repay',true,self::INT,'月供');
            $installment_repay_count  = self::get_param('installment_repay_count',true,self::INT,'已还款月数');
            $installment_handling_bank  = self::get_param('installment_handling_bank',true,self::NOT_EMPTY,'办理银行');

            $installment_loan_money = self::data_filter($installment_loan_money,self::INT,'贷款金额');
            $installment_loan_cycle = self::data_filter($installment_loan_cycle,self::INT,'贷款周期');
            $installment_month_repay = self::data_filter($installment_month_repay,self::DOUBLE,'月供');
            $installment_repay_count = self::data_filter($installment_repay_count,self::INT,'已还款期数');

            $user_car_data['installment_loan_money'] = $installment_loan_money;
            $user_car_data['installment_month_repay'] = $installment_month_repay;
            $user_car_data['installment_loan_cycle'] = $installment_loan_cycle;
            $user_car_data['installment_repay_count'] = $installment_repay_count;
            $user_car_data['installment_handling_bank'] = $installment_handling_bank;

        }else{
            //全款购买
            $pay_type = AssetPayType::full;

            $user_car_data['installment_loan_money'] = ['exp','null'];
            $user_car_data['installment_month_repay'] = ['exp','null'];
            $user_car_data['installment_loan_cycle'] = ['exp','null'];
            $user_car_data['installment_repay_count'] = ['exp','null'];
            $user_car_data['installment_handling_bank'] = ['exp','null'];
        }

        if($is_mortgage == IsMortgage::yes){
            //车辆已抵押
            $mortgage_pay_type = self::get_param('mortgage_pay_type',true,self::NOT_EMPTY,'还款方式');
            $mortgage_start_time  = self::get_param('mortgage_start_time',true,self::NOT_EMPTY,'抵押日期');
            $mortgage_end_time  = self::get_param('mortgage_end_time',true,self::NOT_EMPTY,'抵押到期');
            $mortgage_money = self::get_param('mortgage_money',true,self::DOUBLE,'抵押金额');

            $mortgage_money = self::data_filter($mortgage_money,self::DOUBLE,'抵押金额');

            if(trim($mortgage_start_time) == "") $mortgage_start_time = null;
            if(trim($mortgage_end_time) == "") $mortgage_end_time = null;

            $user_car_data['mortgage_pay_type'] = floatval($mortgage_pay_type);
            $user_car_data['mortgage_money'] = floatval($mortgage_money);
            $user_car_data['mortgage_start_time'] = $mortgage_start_time;
            $user_car_data['mortgage_end_time'] = $mortgage_end_time;
        }else{
            //车辆未抵押
            $is_mortgage = IsMortgage::not;
            $user_car_data['mortgage_pay_type'] = ['exp','null'];
            $user_car_data['mortgage_money'] = ['exp','null'];
            $user_car_data['mortgage_start_time'] = ['exp','null'];
            $user_car_data['mortgage_end_time'] = ['exp','null'];
        }

        $user_car_data['user_id'] = $user_id;
        $user_car_data['car_brand_id'] = $car_brand_id;
        $user_car_data['car_model_id'] = $car_model_id;
        $user_car_data['car_config_id'] = $car_config_id;
        $user_car_data['holder_type'] = $holder_type;
        $user_car_data['run_mileage'] = intval($run_mileage);
        $user_car_data['car_money'] = floatval($car_money);
        $user_car_data['buy_time'] = $buy_time;
        $user_car_data['pay_type'] = $pay_type;
        $user_car_data['is_mortgage'] = $is_mortgage;
        $user_car_data['licence_url'] = $licence_url;
        $user_car_data['car_status'] = CarStatus::Wait;
        $user_car_data['is_history'] = IsHistory::not;
        $user_car_data['create_time'] = get_date_time();

        //开启事物
        IDb::dbStartTrans();

        //添加车辆
        $user_car_add = IDb::getInstance("asset_car")->setDbData($user_car_data)->add();

        //判断添加车辆是否正常
        if($user_car_add === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0001);
            self::set_msg("添加车辆信息失败！");
            self::send();
        }

        //同步个人信息
        $result = User::syncMaterialToUserInfo($user_id,User::MATERIAL_CAR,IsCar::Yes);
        if($result === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0002);
            self::set_msg("添加车辆信息失败！");
            self::send();
        }

        //事物提交
        IDb::dbCommit();

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("user_car_id",$user_car_add);
        self::send();
    }

    /**
     * @功能：编辑个人车辆
     * @开发者：cxl
     */
    public function car_edit(){
        //接收参数
        $user_id = self::$uid;
        $user_car_id  = self::get_data('user_car_id',self::NOT_EMPTY,"车辆编号不能为空！");
        $holder_type  = self::get_data('holder_type',self::INT,'请选择车辆所有人');
        $holder_name  = self::get_data('holder_name');
        $car_brand_id  = self::get_data('car_brand_id',self::INT,'请选择车辆品牌');
        $car_model_id  = self::get_data('car_model_id',self::INT,'请选择车辆型号');
        $car_config_id  = self::get_data('car_config_id',self::INT,'请选择车辆配置');
        $run_mileage  = self::get_param('run_mileage',false,self::DOUBLE,'车辆行驶公里数');
        $car_money  = self::get_param('car_money',true,self::DOUBLE,'裸车价格');
        $buy_time  = self::get_data('buy_time',self::NOT_EMPTY,'请选择购车时间');
        $pay_type  = self::get_data('pay_type',self::INT,'请选择是否有按揭');
        $is_mortgage  = self::get_data('is_mortgage',self::INT,'请选择是否有抵押');
        $licence_url  = self::get_data('licence_url');

        //长度限制
        $car_money = self::data_filter($car_money,self::INT,'裸车价格');
        $run_mileage = self::data_filter($run_mileage,self::INT,'车辆行驶公里数');

        //判断是否设置空值
        if(trim($buy_time) == "") $buy_time = null;
        if(trim($licence_url) == "") $licence_url = null;

        if($holder_type == HolderType::Other){
            //所有人为其它，则取填写的值
            $user_car_data['holder_name'] = $holder_name;
        }

        if($pay_type == AssetPayType::installment){
            //按揭购买

            //数据接收
            $installment_loan_money  = self::get_param('installment_loan_money',true,self::NOT_EMPTY,'贷款金额');
            $installment_loan_cycle  = self::get_param('installment_loan_cycle',true,self::INT,'贷款周期');
            $installment_month_repay  = self::get_param('installment_month_repay',true,self::INT,'月供');
            $installment_repay_count  = self::get_param('installment_repay_count',true,self::INT,'已还款月数');
            $installment_handling_bank  = self::get_param('installment_handling_bank',true,self::NOT_EMPTY,'办理银行');

            $installment_loan_money = self::data_filter($installment_loan_money,self::INT,'贷款金额');
            $installment_loan_cycle = self::data_filter($installment_loan_cycle,self::INT,'贷款周期');
            $installment_month_repay = self::data_filter($installment_month_repay,self::DOUBLE,'月供');
            $installment_repay_count = self::data_filter($installment_repay_count,self::INT,'已还款期数');

            $user_car_data['installment_loan_money'] = $installment_loan_money;
            $user_car_data['installment_month_repay'] = $installment_month_repay;
            $user_car_data['installment_loan_cycle'] = $installment_loan_cycle;
            $user_car_data['installment_repay_count'] = $installment_repay_count;
            $user_car_data['installment_handling_bank'] = $installment_handling_bank;

        }else{
            //全款购买
            $pay_type = AssetPayType::full;

            $user_car_data['installment_loan_money'] = ['exp','null'];
            $user_car_data['installment_month_repay'] = ['exp','null'];
            $user_car_data['installment_loan_cycle'] = ['exp','null'];
            $user_car_data['installment_repay_count'] = ['exp','null'];
            $user_car_data['installment_handling_bank'] = ['exp','null'];
        }

        if($is_mortgage == IsMortgage::yes){
            //车辆已抵押
            $mortgage_pay_type = self::get_param('mortgage_pay_type',true,self::NOT_EMPTY,'还款方式');
            $mortgage_start_time  = self::get_param('mortgage_start_time',true,self::NOT_EMPTY,'抵押日期');
            $mortgage_end_time  = self::get_param('mortgage_end_time',true,self::NOT_EMPTY,'抵押到期');
            $mortgage_money = self::get_param('mortgage_money',true,self::DOUBLE,'抵押金额');

            $mortgage_money = self::data_filter($mortgage_money,self::DOUBLE,'抵押金额');

            if(trim($mortgage_start_time) == "") $mortgage_start_time = null;
            if(trim($mortgage_end_time) == "") $mortgage_end_time = null;

            $user_car_data['mortgage_pay_type'] = floatval($mortgage_pay_type);
            $user_car_data['mortgage_money'] = floatval($mortgage_money);
            $user_car_data['mortgage_start_time'] = $mortgage_start_time;
            $user_car_data['mortgage_end_time'] = $mortgage_end_time;
        }else{
            //车辆未抵押
            $is_mortgage = IsMortgage::not;
            $user_car_data['mortgage_pay_type'] = ['exp','null'];
            $user_car_data['mortgage_money'] = ['exp','null'];
            $user_car_data['mortgage_start_time'] = ['exp','null'];
            $user_car_data['mortgage_end_time'] = ['exp','null'];
        }

        $user_car_data['car_brand_id'] = $car_brand_id;
        $user_car_data['car_model_id'] = $car_model_id;
        $user_car_data['car_config_id'] = $car_config_id;
        $user_car_data['holder_type'] = $holder_type;
        $user_car_data['run_mileage'] = intval($run_mileage);
        $user_car_data['car_money'] = floatval($car_money);
        $user_car_data['buy_time'] = $buy_time;
        $user_car_data['pay_type'] = $pay_type;
        $user_car_data['is_mortgage'] = $is_mortgage;
        $user_car_data['licence_url'] = $licence_url;
        $user_car_data['car_status'] = CarStatus::Wait;
        $user_car_data['is_history'] = IsHistory::not;

        //取得车辆列表
        $user_car_where['user_car_id'] = $user_car_id;
        $user_car_info = IDb::getInstance("asset_car")->setDbWhere($user_car_where)->row();

        //判断是否审核通过
        if(empty($user_car_info)){
            self::set_code(self::WARNING_0001);
            self::set_msg("修改车辆信息失败！");
            self::send();
        }

        //开启事物
        IDb::dbStartTrans();

        //判断是否审核通过
        if($user_car_info['car_status'] == CarStatus::OK){
            //修改车辆信息
            $car_update_data['is_history'] = IsHistory::yes;
            $car_update_data['update_time'] = get_date_time();
            $user_car_upd = IDb::getInstance("asset_car")->setDbWhere($user_car_where)->setDbData($car_update_data)->upd();
            if($user_car_upd === false){
                //数据回滚
                IDb::dbRollback();

                //返回错误
                self::set_code(self::WARNING_0002);
                self::set_msg("修改车辆信息失败！");
                self::send();
            }

            //添加车辆
            $user_car_data['create_time'] = get_date_time();
            $user_car_add = IDb::getInstance("asset_car")->setDbData($user_car_data)->add();
            if($user_car_add === false){
                //数据回滚
                IDb::dbRollback();

                //返回错误
                self::set_code(self::WARNING_0003);
                self::set_msg("修改车辆信息失败！");
                self::send();
            }
        }else {
            //更新车辆信息
            $user_car_data['update_time'] = get_date_time();
            $user_car_upd = IDb::getInstance("asset_car")->setDbWhere($user_car_where)->setDbData($user_car_data)->upd();
            if ($user_car_upd === false) {
                IDb::dbRollback();
                self::set_code(self::WARNING_0004);
                self::set_msg("修改车辆信息失败！");
                self::send();
            }
        }

        //同步个人信息
        $result = User::syncMaterialToUserInfo($user_id,User::MATERIAL_CAR,IsCar::Yes);
        if($result === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0005);
            self::set_msg("修改车辆信息失败！");
            self::send();
        }

        //事物提交
        IDb::dbCommit();

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：删除个人车辆
     * @开发者：cxl
     */
    public function car_del(){
        //接收参数
        $user_id = self::$uid;
        $user_car_id  = self::get_data('user_car_id',self::NOT_EMPTY,"车辆型号编码不能为空！");

        //取得车辆列表
        $user_car_where['user_id'] = $user_id;
        $user_car_where['user_car_id'] = $user_car_id;
        $user_car_data['is_delete'] = IsDelete::Yes;
        $user_car_data['update_time'] = get_date_time();

        IDb::dbStartTrans();
        $user_car_upd = IDb::getInstance("asset_car")->setDbWhere($user_car_where)->setDbData($user_car_data)->upd();

        //判断修改车辆是否正常
        if($user_car_upd === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0002);
            self::set_msg("删除车辆信息失败！");
            self::send();
        }

        //同步个人信息
        $result = User::syncMaterialToUserInfo($user_id,User::MATERIAL_CAR);
        if($result === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0003);
            self::set_msg("同步车辆信息失败！");
            self::send();
        }

        IDb::dbCommit();

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }
}
